//
//  CarSectionTableViewController.m
//  Collection
//
//  Created by satya on 18/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CarSectionTableViewController.h"
#import "DropDownView.h"
#import "dbFunctions.h"
#import "imagePickViewController.h"
#import "SelectImageTableViewController.h"


NSInteger addRows = 1;
UIView *view;

@implementation CarSectionTableViewController
@synthesize carSectionBtn,arrTxtFdData,dictextVal;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	
	UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Add More",@"Select Images",nil]];
	control.segmentedControlStyle = UISegmentedControlStyleBar;
	control.momentary = YES;
	[control addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];			
	UIBarButtonItem *controlItem = [[UIBarButtonItem alloc] initWithCustomView:control];
	self.navigationItem.rightBarButtonItem = controlItem;
	[control release];
	[controlItem release];
	dictextVal = [[NSMutableDictionary alloc]init]; 
	carSectionBtn = [[NSMutableArray alloc]initWithObjects:@"",nil];
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	view = [[UIView alloc]init];
    [self.tableView addSubview:view]; 
     view.hidden = YES;
}

-(void) segmentValueChanged:(id) sender{
    UISegmentedControl *segmentCtrl = (UISegmentedControl *)sender;
	if(segmentCtrl.selectedSegmentIndex == 0){
		addRows++;
		[carSectionBtn addObject:@""];

		[self performSelector:@selector(reloadTable) withObject:nil afterDelay:0.3];
		
		self.tableView.frame = CGRectMake(0,0,self.tableView.frame.size.width,self.tableView.frame.size.height+200);
		self.tableView.contentSize = CGSizeMake(self.tableView.contentSize.width,self.tableView.contentSize.height+500);
		self.view.frame= CGRectMake(0,0,self.tableView.contentSize.width,self.tableView.contentSize.height+500);
	}
	else {
		NSLog(@"%@",carSectionBtn);
		imagePickViewController *carSec = [[imagePickViewController alloc]initWithWebData:carSectionBtn];
		[self.navigationController pushViewController:carSec animated:YES];
		[carSec release];		
		 	
	}
}



/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
	//[self handleInterfaceRotationForOrientation:interfaceOrientation];

	return YES;
}




#pragma mark -
#pragma mark Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
   
	return addRows;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
	
    UIButton *btnItem = [UIButton buttonWithType:UIButtonTypeCustom];
	   [btnItem setBackgroundImage:[UIImage imageNamed:@"list.PNG"] forState:UIControlStateNormal];
	if ([[carSectionBtn objectAtIndex:indexPath.row] length]>0) {
		[btnItem setTitle:[carSectionBtn objectAtIndex:indexPath.row] forState:UIControlStateNormal];
		btnItem.titleLabel.font = [UIFont systemFontOfSize:10.0]; 

	}
	else {
		[btnItem setTitle:@"car section" forState:UIControlStateNormal];

	}

    [btnItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[btnItem addTarget:self action:@selector(selectRowNavigate:) forControlEvents:UIControlEventTouchUpInside];
 
	btnItem.frame = CGRectMake(3, 6, 162, 35); 
    btnItem.tag = indexPath.row;
	//selectedBtn = indexPath.row;
    btnItem.titleLabel.font = [UIFont fontWithName:@"Arial" size:16];
	[cell.contentView addSubview:btnItem];
	
	UITextField *txtAccDamage = [[UITextField alloc]initWithFrame:CGRectMake(5, 45, 156, 33)];//Accident Damage
	txtAccDamage.borderStyle = UITextBorderStyleRoundedRect;
	txtAccDamage.placeholder = @"Accident Damage";
	txtAccDamage.font = [UIFont systemFontOfSize:13];
	txtAccDamage.delegate = self;
	txtAccDamage.tag = 301;
	[cell.contentView addSubview:txtAccDamage];
    [txtAccDamage release];
	
	UITextField *txtAccNonRelated = [[UITextField alloc]initWithFrame:CGRectMake(170, 7, 145, 33)];//Non Related Accident
	txtAccNonRelated.borderStyle = UITextBorderStyleRoundedRect;
	txtAccNonRelated.placeholder = @"Non Related Accident";
	txtAccNonRelated.font = [UIFont systemFontOfSize:13];
	txtAccNonRelated.tag = 302;

	txtAccNonRelated.delegate = self;


	[cell.contentView addSubview:txtAccNonRelated];
    [txtAccNonRelated release];
	
	UITextField *txtPrivateQuot = [[UITextField alloc]initWithFrame:CGRectMake(170, 45, 145, 33)];//Private Quotation Area
	txtPrivateQuot.borderStyle = UITextBorderStyleRoundedRect;
	txtPrivateQuot.placeholder = @"Private Quote Area";
	txtPrivateQuot.tag = 303;

	txtPrivateQuot.delegate = self;
	txtPrivateQuot.font = [UIFont systemFontOfSize:13];
   [cell.contentView addSubview:txtPrivateQuot];
    [txtPrivateQuot release];
	
	
	
    return cell;
}

  NSInteger selectedBtn;
-(void)selectRowNavigate:(UIButton *)sender
{
	dbFunctions *getlist = [[dbFunctions alloc]init];
	NSLog(@"%@",[getlist getPDF]);
	
	selectedBtn = sender.tag;
	//neelam dropdown
	NSMutableArray *arr = [getlist getPDF];
	DropDownView *dropDownView = [[DropDownView alloc] initWithArrayData:arr cellHeight:30 heightTableView:100 paddingTop:0 paddingLeft:-5 paddingRight:-10 refView:sender animation:BLENDIN openAnimationDuration:.5 closeAnimationDuration:.5];
	
    dropDownView.view.frame =  CGRectMake(sender.frame.origin.x+3,(40*(selectedBtn*2+1)+selectedBtn*2)-2, sender.frame.size.width, 200);

	[dropDownView openAnimation];
	dropDownView.delegate = self;
	
	[self.tableView addSubview:dropDownView.view];
	//[dropDownView release];
	//if (sender.tag == 0) {
	
	/*///Simple DropDown
	SelectImageTableViewController *selct = [[SelectImageTableViewController alloc]init];
	selct.sender = self;
	selct.selector = @selector(updateButtonTitle:);
	//selct.view.frame = CGRectMake(0, 0, 150, 200);
	view.frame = CGRectMake(sender.frame.origin.x,(40*(selectedBtn*2+1)+selectedBtn*2), sender.frame.size.width, 150);
  //  (sender.frame.origin.y+sender.frame.size.height)+(self.tableView.rowHeight*sender.tag)
	selct.view.frame  = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
	[view setHidden:NO];
	[view addSubview:selct.view];
	 ///Simple DropDown	   */  
	
	//}
    
	/*if (sender.tag == 1) {
		SelectImageTableViewController *selct = [[SelectImageTableViewController alloc]init];
		selct.view.frame = CGRectMake(10, 80, 150, 200);
		[self.tableView addSubview:selct.view];
	}*/
	


}

#pragma mark -
#pragma mark TextField Delegate

-(BOOL)textFieldShouldReturn:(UITextField *)txtField{
    [txtField resignFirstResponder];
    return YES;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
	return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
		
	
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
	NSMutableDictionary *dic;
	if (textField.tag==301) {
		
		[dic setObject:textField.text forKey:@"301"];
	}
	
	if (textField.tag==302) {
		
		[dic setObject:textField.text forKey:@"302"];
	}
	
	if (textField.tag==302) {
		
		[dic setObject:textField.text forKey:@"303"];
	}
	
	
}

-(void)updateButtonTitle:(NSString *)title
{
	NSLog(@"%@",title);
	[view setHidden:YES];
	[carSectionBtn replaceObjectAtIndex:selectedBtn withObject:title];
	[self.tableView reloadData];



}


	 -(void)reloadTable
	 {
		 [self.tableView reloadData];
	 
	 }


#pragma mark -
#pragma mark DropDownViewDelegate

-(void)dropDownCellSelected:(NSInteger)returnIndex{
	//[driverNameBtn_ setTitle:[drivableArr_ objectAtIndex:returnIndex] forState:UIControlStateNormal];
	//driverNameBtn_.tag = returnIndex;
	dbFunctions *getlist = [[dbFunctions alloc]init];
	NSMutableArray *arr = [getlist getPDF];
	NSLog(@"Selected item ~~%@",[arr objectAtIndex:returnIndex]);
	[carSectionBtn replaceObjectAtIndex:selectedBtn withObject:[arr objectAtIndex:returnIndex]];
	[self.tableView reloadData];
	
	
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 83;

}






#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[arrTxtFdData release];
	[carSectionBtn release];
	[dictextVal release];
}


@end

